package com.crm.controller.adminController;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import com.crm.dao.BusinessDAO;
import com.crm.entity.Business;
@Controller
@RequestMapping("/company")
public class BusinessController {
	protected final Log logger = LogFactory.getLog(getClass());
	BusinessDAO busDAO=new BusinessDAO();
	
	@RequestMapping("/open.do")
	public ModelAndView open(HttpSession session,ModelAndView model){
		Business bus=(Business)session.getAttribute("business");
		if(bus==null)return new ModelAndView("/jsp/index.jsp");
		if(bus!=null)
		{
		 model= new ModelAndView("/jsp/company.jsp", "command", bus);
		}
		model.addObject("submenu", "admin");
		model.addObject("menuName","admin");
		return model;
		
	}
	@RequestMapping("/save.do")
	public ModelAndView saveCompany(HttpSession session,HttpServletRequest request,HttpServletResponse response,ModelAndView model) throws Exception{
		Business bus=(Business)session.getAttribute("business");
		if(bus==null)return new ModelAndView("/jsp/index.jsp");
		byte[] filecontents=null;
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		MultipartFile file = multipartRequest.getFile("logo");
        logger.info("File:" + file.getName());
        logger.info("ContentType:" + file.getContentType());
        try {
        	  filecontents=file.getBytes();
        	logger.info("Contents before");
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		String compname=(String) request.getParameter("name");
		String address=(String) request.getParameter("address");
		String tinNo=(String) request.getParameter("tinNo");
		String cstNo=(String) request.getParameter("cstNo");
		String phoneNo=(String) request.getParameter("phone");
		String panNo=(String) request.getParameter("panNo");
		String shortName=(String) request.getParameter("shortName");
		String fax=(String) request.getParameter("fax");
		String ifsc=(String) request.getParameter("ifsc");
		String email=(String) request.getParameter("email");
		String accountNo=(String) request.getParameter("accountNo");
		String micrNo=(String) request.getParameter("micr");
		String terms=(String) request.getParameter("tAndC");
		
		
		int accno=0;
		if (accountNo!=null) {
			accno=Integer.parseInt(accountNo);
		}
		int micr=0;
		if (micrNo!=null) {
			micr=Integer.parseInt(micrNo);
		}
		
		Business b=new Business(compname,accno,address,cstNo,email,fax,ifsc,bus.getBusinessId(),micr,panNo,phoneNo,shortName,terms,tinNo,filecontents);
		String msg=null;
		
		try{
			busDAO.insert(b);
			msg="Company is sucessfully Inserted.Thankyou";
		}catch(Exception e){
			e.printStackTrace();
			msg="Company is Failed To  Insert.Please Try Again.";
		}
		return model=new ModelAndView("/jsp/company.jsp")
		.addObject("menuName","admin")
		.addObject("submenu","admin")		
		.addObject("success-msg",msg);
	}
	@RequestMapping("/edit.do")
	public ModelAndView editCompany(HttpSession session,HttpServletRequest request,HttpServletResponse response) throws Exception{
		Business bus=(Business)session.getAttribute("business");
		if(bus==null)return new ModelAndView("/jsp/index.jsp");
		BusinessDAO c=new BusinessDAO();
		List<Business> lc=c.getAllCompanies();
		
		ModelAndView model = new ModelAndView("/jsp/company.jsp");
		model.addObject("company-List",lc);
		model.addObject("submenu", "admin");
		model.addObject("menuName","admin");
		return model;
	}
	@RequestMapping("/search.do")
	public ModelAndView searchCompany(HttpSession session,HttpServletRequest request,HttpServletResponse response) throws Exception{
		Business bus=(Business)session.getAttribute("business");
		if(bus==null)return new ModelAndView("/jsp/index.jsp");
		
		Business lc=busDAO.getShrt(bus.getBusinessId(),bus.getBusinessName());
		
		ModelAndView model = new ModelAndView("/jsp/editCompanyRes.jsp");
		model.addObject("company",lc);
		
		return model;
	}
	@RequestMapping("/editsave.do")
	public ModelAndView saveEdit(HttpSession session,ModelAndView model, BindingResult result,HttpServletRequest request,HttpServletResponse response,Business bus) throws Exception{
		
		logger.info("Business is:"+bus);
		String msg=null;
		model=new ModelAndView("/jsp/company.jsp");
		try{
			System.out.println("Business Terms And Conditions Are : "+bus.getTandc());
			busDAO.insert(bus);
			session.setAttribute("business", bus);
			msg="Company Details Are  Successfully Updated.Thankyou";
		}catch(Exception e){
			e.printStackTrace();
			msg="Company is Failed To  Update Data.Please Try Again.";
		}
		if(bus!=null)
		{
		 model= new ModelAndView("/jsp/company.jsp", "command", new Business());
		}
		model.addObject("submenu", "admin");
		model.addObject("menuName","admin");
		model.addObject("success-msg",msg);
		return model;
	}
}

